package com.wish.housing.service.impl;

import com.wish.housing.entity.EsRoomInfo;
import com.wish.housing.es.EsRoomRinfoRepository;
import com.wish.housing.mapper.EsTbRoomInfoMapper;
import com.wish.housing.service.EsTbRoomInfoService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Wish-Cloud-master
 * @description:
 * @author: Mr.Wang
 * @create: 2022-03-29 15:46
 **/
@Service
public class EsTbRoomInfoServiceImpl implements EsTbRoomInfoService {

    @Autowired
    EsTbRoomInfoMapper esTbRoomInfoMapper;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    EsRoomRinfoRepository esRoomRinfoRepository;

    @Override
    public Page<EsRoomInfo> list(EsRoomInfo roomInfo, Integer pageNum, Integer pageSize) {

        long count = esRoomRinfoRepository.count();
        if (count < 1) {
            //获取所有数据
            List<EsRoomInfo> list = esTbRoomInfoMapper.list();
            esRoomRinfoRepository.saveAll(list);
        }

        PageRequest of = PageRequest.of(pageNum - 1, pageSize);

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //设置高亮前缀后缀
        String pre = "<font color='red'>";
        String post = "</font>";

        HighlightBuilder.Field roomName = new HighlightBuilder.Field("roomName").preTags(pre).postTags(post);
        HighlightBuilder.Field name = new HighlightBuilder.Field("name").preTags(pre).postTags(post);

        // 判断出租状态是否为空
        if (null != roomInfo.getStatus()) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("status", roomInfo.getStatus()));
        }

        //判断发布状态
        if (null != roomInfo.getPublishStatus()) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("publishStatus", roomInfo.getPublishStatus()));
        }

        // 判断房源名称
        if (StringUtils.isNotBlank(roomInfo.getRoomName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("roomName", roomInfo.getRoomName()));
        }

        //判断楼盘名称
        if (StringUtils.isNotBlank(roomInfo.getName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("name", roomInfo.getName()));
        }

        NativeSearchQuery build = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(of)
                .withHighlightFields(roomName, name)
                .build();

        //从es中获取数据
        SearchHits<EsRoomInfo> search = elasticsearchRestTemplate.search(build, EsRoomInfo.class);
        //获取总条数
        long totalHits = search.getTotalHits();
        List<SearchHit<EsRoomInfo>> searchHits = search.getSearchHits();
        ArrayList<EsRoomInfo> esRoomInfos = new ArrayList<>();
        for (SearchHit<EsRoomInfo> searchHit : searchHits) {
            // 获取对象
            EsRoomInfo content = searchHit.getContent();
            // 判断是否有高亮内容
            if (searchHit.getHighlightField("name") != null && searchHit.getHighlightField("name").size() > 0) {
                content.setName(searchHit.getHighlightField("name").get(0));
            }
            if (searchHit.getHighlightField("roomName") != null && searchHit.getHighlightField("roomName").size() > 0) {
                content.setRoomName(searchHit.getHighlightField("roomName").get(0));
            }
            esRoomInfos.add(content);
        }
        Page<EsRoomInfo> page = new PageImpl<>(esRoomInfos, of, totalHits);

        return page;
    }

    @Override
    public EsRoomInfo findRoomInfoById(String id) {
        return esTbRoomInfoMapper.findRoomInfoById(id);
    }

    @Override
    public void updateEs() {
        esRoomRinfoRepository.deleteAll();
        List<EsRoomInfo> list = esTbRoomInfoMapper.list();
        esRoomRinfoRepository.saveAll(list);
    }
}
